import time

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support.select import Select
from selenium.webdriver.support import expected_conditions as ec
from selenium.webdriver.common.action_chains import ActionChains
class Base():
    def __init__(self,driver):
        self.driver=driver
    def goto(self,url):
        self.driver.get(url)
    #     显示等待
    def find_element(self,loc):
        try:
            WebDriverWait(self.driver,20,0.5).until(lambda driver:driver.find_element(*loc).is_displayed)
            return self.driver.find_element(*loc)
        except Exception as e:
            print(e)
    # 封装点击
    def click(self,loc):
        self.find_element(loc).click()
        #     封装输入
    def send_keys(self,loc,text):
        self.find_element(loc).send_keys(text)
    #     封装文本
    def text(self,loc):
        return self.find_element(loc).text
    # 强制等待
    def sleep(self,timeout):
        time.sleep(int(timeout))
        # 隐式等待
    def wait(self,loc):
        self.driver.implicitly_wait(loc)
    # 滚动条
    def gd(self):
        self.driver.execute_script('window-scrollTo(0,10000)')
    # 弹窗
#     进入框架
    def frame(self,loc):
        self.driver.switch_to.frame(self.find_element(loc))
#     退出框架
    def tframe(self):
        self.driver.switch_to.default_content()
#     窗口切换
    def qh(self):
        self.driver.switch_to.window(self.driver.window_handles[-1])
#     鼠标事件
#     悬浮
    def move_click(self,loc):
        ActionChains(self.driver).move_to_element(self.find_element(loc)).perform()
    #     拖动
    def drop_click(self,loc):
        ActionChains(self.driver).drag_and_drop(self.find_element(loc)).perform()
    #     按住不放
    def hold_click(self,loc):
        ActionChains(self.driver).click_and_hold(self.find_element(loc)).perform()
#     三击
    def double_click(self,loc):
        ActionChains(self.driver).double_click(self.find_element(loc)).click(self.find_element(loc)).perform()
#     双击
    def double(self,loc):
        ActionChains(self.driver).double_click(self.find_element(loc)).perform()
#     键盘事件
    def enter(self,loc):
        self.find_element(loc).send_keys(Keys.ENTER)
#     下拉框
    def select(self,loc,x):
        select2=Select(self.find_element(loc))
        select2.select_by_value(x)
#     窗口最大化
    def max(self):
        self.driver.maximize_window()
#     页面刷新
    def ref(self):
        self.driver.refresh()
# #     异常截图
#     def jt(self):
#         times=time.strftime('%Y%m%d_%H%M%S',time.localtime())
#         return self.driver.save_screenshot('hj'+times+'.png')


